<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-button type="primary" @click="(dialogFormVisible = true), add()"
        >新增</el-button
      >

      <!-- 弹出框 -->
      <el-dialog title="新增公告" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="操作员" :label-width="formLabelWidth">
            <el-select v-model="form.name" placeholder="请选择管理员">
              <el-option
                v-for="item in gly"
                :key="item._id"
                :value="item.username"
                >{{ item.username }}</el-option
              >
            </el-select>
          </el-form-item>
          <el-form-item label="公告内容" :label-width="formLabelWidth">
            <el-input v-model="form.content" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="(dialogFormVisible = false), yes(form)"
            >确 定</el-button
          >
        </div>
      </el-dialog>

      <el-form-item>
        <el-select v-model="formInline.region" placeholder="查询区域">
          <el-option label="操作人员" value="renyuan"></el-option>
          <el-option label="发布内容" value="fabu"></el-option>
        </el-select>
        <el-form-item>
          <el-input v-model="formInline.user" placeholder="查询内容"></el-input>
        </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="announcements"
      border
      style="width: 100%"
      v-loading="loading"
    >
      <el-table-column prop="name" label="操作人员"> </el-table-column>
      <el-table-column prop="content" label="发布内容"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="
              handleEdit(scope.$index, scope.row), (dialogFormVisible1 = true)
            "
            >编辑</el-button
          >

          <el-dialog title="修改公告" :visible.sync="dialogFormVisible1">
            <el-form :model="form1">
              <el-form-item label="操作员" :label-width="formLabelWidth">
                <el-select v-model="form1.name">
                  <el-option
                    v-for="item in gly"
                    :key="item._id"
                    :value="item.username"
                    >{{ item.username }}</el-option
                  >
                </el-select>
              </el-form-item>
              <el-form-item label="公告内容" :label-width="formLabelWidth">
                <el-input v-model="form1.content" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible1 = false">取 消</el-button>
              <el-button
                type="primary"
                @click="(dialogFormVisible1 = false), gai(form1)"
                >确 定</el-button
              >
            </div>
          </el-dialog>

          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  getAllAnnouncements,
  deleteAnnouncements,
  getGLY,
  addGLY,
  gaiGLY,
} from "@/services/announcementsServices";
export default {
  methods: {
    // 修改
    async handleEdit(index, row) {
      console.log(index, row);
      let resp = await getGLY();
      this.gly = resp.rows.filter((item) => item.privilege === 1);
      this.form1 = {
        _id: row._id,
        name: row.name,
        content: row.content,
      };
    },
    // 删除
    async handleDelete(index, row) {
      this.loading = true;
      console.log(index, row);
      await deleteAnnouncements(row._id);
      this.loading = !this.loading;
      alert("删除成功")
      this.inquire();
    },
    // 查询
    async onSubmit() {
      console.log(this.formInline.region);
      if (this.formInline.region == "renyuan") {
        let resp = await getAllAnnouncements();
        console.log(resp);
        console.log(this.formInline.user);
        this.announcements = resp.rows.filter(
          (item) => item.name == this.formInline.user
        );
        console.log(this.announcements);
      } else if (this.formInline.region == "fabu") {
        let resp = await getAllAnnouncements();
        this.announcements = resp.rows.filter(
          (item) => item.content == this.formInline.user
        );
        console.log(this.announcements);
      }
    },
    async add() {
      let resp = await getGLY();
      this.gly = resp.rows.filter((item) => item.privilege === 1);
    },
    // 新增
    async yes(form) {
        
      console.log(form);
      this.loading = true;
      let resp = await addGLY(form);
      this.loading = !this.loading;
      console.log("resp", resp);
      this.form= {
        name: "",
        content: "",
      },
      this.inquire();
    },
    //修改
    async gai(form1) {
      console.log(form1);
      this.loading = true;
      let resp = await gaiGLY(form1);
      this.loading = !this.loading;
      console.log(resp);
      this.inquire();
    },
    async inquire() {
      this.loading = true;
      let resp = await getAllAnnouncements();
      this.announcements = resp.rows;
      this.loading = !this.loading;
    },
  },
  data() {
    return {
      loading: true,
      announcements: [],
      gly: [],
      formInline: {
        user: "",
        region: "",
      },
      //   dialogTableVisible: false,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        name: "",
        content: "",
      },
      form1: {
        _id: "",
        name: "",
        content: "",
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    this.inquire();
  },
};
</script>

<style>
/* .el-button--primary{
    margin-right: 20px;
}
.el-main{
    text-align:left;
}
.el-form--inline .el-form-item{
    margin-right: 0px;
} */
</style>